package com.shinowit.web;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shinowit.dao.mapper.SupplierInfoMapper;
import com.shinowit.model.DataGridResult;
import com.shinowit.model.Result;
import com.shinowit.model.SupplierInfo;
import com.shinowit.model.SupplierInfoCriteria;
import com.shinowit.tools.LetterUtil;

@Controller
@RequestMapping(value="/supplier_info")
public class SupplierInfoController {

	@Resource
	private SupplierInfoMapper supplier_info_dao;
	

	@RequestMapping(value={"","/"},method=RequestMethod.GET)
	public String defaultPage(){
		return "/supplier_info/list";
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public DataGridResult<SupplierInfo> getPage(@RequestParam(value="page",defaultValue="1") Integer pageIndex,@RequestParam(value="rows",defaultValue="10") Integer pageSize,SupplierInfo supplier_info,String sort,String order){
		DataGridResult<SupplierInfo> result=new DataGridResult<SupplierInfo>();
		
		SupplierInfoCriteria ex=new SupplierInfoCriteria();
		SupplierInfoCriteria.Criteria cr1= ex.createCriteria();
		
		if (null!=supplier_info){
			if ((supplier_info.getSupplierName()!=null) && (supplier_info.getSupplierName().trim().length()>0)){
				cr1.andSupplierNameLike(supplier_info.getSupplierName()+"%");
			}
			if ((supplier_info.getSupplierAb()!=null) && (supplier_info.getSupplierAb().trim().length()>0)){
				cr1.andSupplierAbLike(supplier_info.getSupplierAb()+"%");
			}
		}
		
		ex.setPageIndex(pageIndex);
		ex.setPageSize(pageSize);
		
		if ((null!=sort) && (sort.trim().length()>0)){
			ex.setOrderByClause(sort);
		}
		
		if ((null!=order) && (order.trim().length()>0)){
			if (!"asc".equalsIgnoreCase(order)){
				ex.setOrderByClause(ex.getOrderByClause()+" desc");
			}
		}
		
		List<SupplierInfo> data_list=supplier_info_dao.selectByExamplePage(ex);
		result.setRows(data_list);
		
		long record_count=supplier_info_dao.countByExample(ex);
		result.setTotal(record_count);
		
		return result;
	}	
	

	@RequestMapping(value="/create")
	@ResponseBody
	public Result create_new(SupplierInfo data){
		Result result=new Result();
		
		data.setSupplierAb(LetterUtil.getAllFirstLetter(data.getSupplierName()));
		
		int i=supplier_info_dao.insert(data);
		if (1==i){
			result.setMsg("保存成功！");
			result.setStatus(true);
		}else{
			result.setMsg("保存失败！");
			result.setStatus(false);
		}
		return result;
	}

	@RequestMapping(value="/edit")
	@ResponseBody
	public Result edit_data(SupplierInfo data){
		Result result=new Result();
		data.setSupplierAb(LetterUtil.getAllFirstLetter(data.getSupplierName()));
		int i=supplier_info_dao.updateByPrimaryKey(data);
		if (1==i){
			result.setMsg("修改成功！");
			result.setStatus(true);
		}else{
			result.setMsg("修改失败！");
			result.setStatus(false);
		}
		return result;
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Result delete_data(String id){
		Result result=new Result();
		int i=supplier_info_dao.deleteByPrimaryKey(id);
		if (1==i){
			result.setMsg("删除成功！");
			result.setStatus(true);
		}else{
			result.setMsg("删除失败！");
			result.setStatus(false);
		}
		return result;
	}				
	
}
